package com.kingyea.mobilepolice.apimanage.serviceinfo.controller;

import com.kingyea.mobilepolice.apimanage.log.OperateType;
import com.kingyea.mobilepolice.apimanage.log.OperationLogAnnotation;
import com.kingyea.mobilepolice.apimanage.provider.vo.ProviderVO;
import com.kingyea.mobilepolice.apimanage.serviceinfo.bean.ServiceInfo;
import com.kingyea.mobilepolice.apimanage.serviceinfo.service.ServiceInfoService;
import com.kingyea.mobilepolice.apimanage.serviceinfo.vo.ServiceInfoVO;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import com.kingyea.mobilepolice.user.client.UserServiceClient;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description ServiceInfo 控制层
 * @date Created in 2018-03-02 16:37:14
 */
@Api(value = "服务管理接口 ", description = " 服务管理相关 ")
@RequestMapping(value = "/serviceInfo")
@RestController
public class ServiceInfoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInfoController.class);

    @Autowired
    private ServiceInfoService serviceInfoService;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 根据搜索条件搜索接口列表
     *
     * @param map
     * @return com.device.mall.utils.page.PageInfo
     * @author Mr.Lin
     * @date 2017/11/20 15:57
     */
    @ApiOperation(value = "根据搜索条件搜索接口列表", notes = "", response = ServiceInfo.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status", value = " 状态：1:注册未审核，2:变更未审核，3:审核通过，4:审核不通过 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "每页数据数量大小", required = true, dataType = "int")
            , @ApiImplicitParam(name = "keyword", value = " 关键字keyword，订单号和代理商名字 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "searchType", value = " 搜索类型（name.接口名称 serialNo.接口标识 organization.管理单位名称  all.所有） ", required = true, dataType = "String")
            , @ApiImplicitParam(name = "policeType", value = " 分类id 列表,对应服务的警种类型 ", required = false, dataType = "Array")
            // 以下参数一般用于适应关联页面列表里边的筛选
            , @ApiImplicitParam(name = "isDel", value = " 是否删除：0.否 1.是 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "isOnline", value = " 是否上线：0.否 1.是 ", required = false, dataType = "Integer")
    })
    @OperationLogAnnotation(operateName = "根据搜索条件搜索接口列表", operateType = OperateType.SELECT, module = "serviceinfo")
    @RequestMapping(value = "/searchByCondition", method = RequestMethod.POST)
    public Object searchByCondition(@RequestBody Map<String, Object> map) throws Exception {
        if (map.containsKey("roleId")) {
            if (!"".equals(map.get("roleId")) && Integer.parseInt(map.get("roleId").toString()) == 0) {
                map.put("idAdmin", null);
            } else {
                map.put("isAdmin", userServiceClient.getCurrentUserInfo().getData().getUserId());
            }
        }
        if (map.containsKey("status")) {
            if (!"".equals(map.get("status")) && Integer.parseInt(map.get("status").toString()) == 0) {
                map.put("status", 1);
                map.put("_status", 2);
            }
        }
        return serviceInfoService.searchByCondition(map);
    }

    /**
     * 禁用启用、上下线
     *
     * @param map
     * @return com.device.mall.utils.page.PageInfo
     * @author Mr.Lin
     * @date 2017/11/20 15:57
     */
    @ApiOperation(value = "禁用启用、上下线", notes = "", response = ServiceInfo.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = " 接口ID ", required = true, dataType = "int")
    })
    @OperationLogAnnotation(operateName = "接口上下线", operateType = OperateType.UPDATE, module = "serviceinfo")
    @RequestMapping(value = "/changeOnLineStatus", method = RequestMethod.POST)
    public Object changeOnLineStatus(@RequestBody Map<String, Object> map) throws Exception {
        return serviceInfoService.changeOnLineStatus(map);
    }

    /**
     * 审核
     *
     * @param map
     * @return int
     * @author Mr.Lin
     * @date 2018/3/9 15:51
     */
    @ApiOperation(value = "审核", notes = "", response = Integer.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int")
            , @ApiImplicitParam(name = "status", value = "审核状态：1:注册未审核，2:变更未审核，3:审核通过，4:审核不通过", required = true, dataType = "int")
    })
    @OperationLogAnnotation(operateName = "接口审核", operateType = OperateType.UPDATE, module = "serviceinfo")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public Object check(@RequestBody Map<String, Object> map) throws Exception {
        return serviceInfoService.check(map);
    }

    /**
     * 添加接口
     *
     * @param serviceInfoVO
     * @return
     */
    @ApiOperation(value = " 添加接口 ", notes = "", response = ServiceInfo.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = " 接口名称", required = true, dataType = "String")
    })
    @OperationLogAnnotation(operateName = "添加接口", operateType = OperateType.INSERT, module = "serviceinfo")
    @RequestMapping(method = RequestMethod.POST)
    public Object create(@RequestBody @ApiParam(name = "接口对象", value = "传入json格式", required = true) ServiceInfoVO serviceInfoVO) throws Exception {
        Result<User> result = userServiceClient.getCurrentUserInfo();
        serviceInfoVO.setCreateById(result.getData().getUserId());
        serviceInfoVO.setCreateByName(result.getData().getName());
        return serviceInfoService.saveVO(serviceInfoVO);
    }


    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @OperationLogAnnotation(operateName = "查询全部接口", operateType = OperateType.SELECT, module = "serviceinfo")
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return serviceInfoService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @OperationLogAnnotation(operateName = "通过ID查找接口", operateType = OperateType.SELECT, module = "serviceinfo")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object detail(@PathVariable Integer id) throws Exception {
        return serviceInfoService.getById(id);
    }

    /**
     * 更新
     *
     * @param serviceInfoVO
     * @return
     */
    @OperationLogAnnotation(operateName = "更新接口", operateType = OperateType.UPDATE, module = "serviceinfo")
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@RequestBody ServiceInfoVO serviceInfoVO) throws Exception {
        Result<User> result = userServiceClient.getCurrentUserInfo();
        serviceInfoVO.setModifiedById(result.getData().getUserId());
        return serviceInfoService.updateVO(serviceInfoVO);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @OperationLogAnnotation(operateName = "删除接口", operateType = OperateType.DELETE, module = "serviceinfo")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Object delete(@PathVariable Integer id) throws Exception {
        return serviceInfoService.deleteById(id);
    }


    /**
     * 带条件统计接口数量
     *
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "带条件统计接口数量", notes = "", response = ProviderVO.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startTime", value = "注册开始时间", required = true, dataType = "Date")
            , @ApiImplicitParam(name = "endTime", value = "注册结束时间", required = true, dataType = "Date")

    })
    @OperationLogAnnotation(operateName = "带条件统计接口数量", operateType = OperateType.SELECT, module = "requester")
    @RequestMapping(value = "/countByCondition", method = RequestMethod.POST)
    public Object countByCondition(@RequestBody Map<String, Object> map) throws Exception {
        Object data = serviceInfoService.countByCondition(map);
        return data;
    }

    /**
     * 每个接口的总调用次数列表searchByCondition
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/countAllService", method = RequestMethod.POST)
    public Object countAllService(@RequestBody Map<String, Object> map) throws Exception {
        Object data = serviceInfoService.countAllService(map);
        return data;
    }

    /**
     * 每个接口今日调用次数列表
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/countTodayService", method = RequestMethod.POST)
    public Object countTodayService(@RequestBody Map<String, Object> map) throws Exception {
        Object data = serviceInfoService.countTodayService(map);
        return data;
    }

    /**
     * 每个网络各种统计
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/serviceInfoCountByLoction", method = RequestMethod.POST)
    public Object serviceInfoCountByLoction(@RequestBody Map<String, Object> map) throws Exception {
        Object data = serviceInfoService.serviceInfoCountByLoction(map);
        return data;
    }


    /**
     * 根据搜索条件搜索接口和提供方列表
     *
     * @param map
     * @return com.device.mall.utils.page.PageInfo
     * @author Mr.Lin
     * @date 2017/11/20 15:57
     */
    @ApiOperation(value = "根据搜索条件搜索接口列表", notes = "", response = ServiceInfo.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "每页数据数量大小", required = true, dataType = "int")
            , @ApiImplicitParam(name = "serviceName", value = " 服务名称 ", required = false, dataType = "String")
            , @ApiImplicitParam(name = "serviceSerialNo", value = " 服务标识 ", required = true, dataType = "String")
            , @ApiImplicitParam(name = "providerName", value = " 服务方名称 ", required = false, dataType = "String")
            , @ApiImplicitParam(name = "providerSerialNo", value = " 服务方标识 ", required = true, dataType = "String")
    })
    @OperationLogAnnotation(operateName = "根据搜索条件搜索接口和提供方列表", operateType = OperateType.SELECT, module = "serviceinfo")
    @RequestMapping(value = "/searchServiceAndProvider", method = RequestMethod.POST)
    public Object searchServiceAndProvider(@RequestBody Map<String, Object> map) throws Exception {
        return serviceInfoService.searchServiceAndProvider(map);
    }

}
